/*
 *退货单详情页面
 */

var sysReturnDetail = {};
sysReturnDetail.operation = {};
$sysReturnSkuTable = null;
$sysFactReturnSkuTable = null;

$(function () {
    // 根据退货单ID获取退货明细信息
    var returnData = sysReturnDetail.getDetailInfoByReturnId();
    if (!!returnData) {
        // 初始化基本信息页
        sysReturnDetail.initBasePage(returnData);
        // 初始化计划退入商品信息表格
        sysReturnDetail.initReturnSkuList(returnData);
        // 初始化实际退入商品信息表格
        sysReturnDetail.initFactReturnSku(returnData);
        // 初始化内部便签
        sysReturnDetail.initNotePage(returnData);
    }

    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {

        switch ($(this).attr("id")) {
            case "noteInfoTabLink":{

                window.frames["noteIframe"].window.$noteTable.ajax.reload().draw();
                break;
            }

        };
    });

});

sysReturnDetail.initBasePage = function (pageData) {
    // 基本信息页退货信息赋值
    for (var index in pageData) {
        $("#tab-basicInfo").find("#" + index).html(pageData[index]);
    }

    // 基本信息页退货信息赋值
    for (var index in pageData.sysReturnVO) {
        var $Obj = $("#tab-basicInfo").find("#" + index);
        if (!$Obj.html()) {
            $("#tab-basicInfo").find("#" + index).html(pageData.sysReturnVO[index]);
        }
    }
};
sysReturnDetail.initReturnSkuList = function (pageData) {
    // 根据退货ID初始化计划退入商品明细列表
    if (null == $sysReturnSkuTable) {
        $sysReturnSkuTable = $("#sysReturnSkuTable").DataTable({
            "processing": true,
            "dom": '<"bottom"rtflp>',
            "searching": false,
            "scrollCollapse": false,
            "destroy": true,
            "paging": false,
            "deferRender": true,
            "ordering": false,
            "columns": [{
                "data": "identification",
                "className": "truncate-text tooltip-column-detail text-left",
                "render": function ( data, type, row, meta ) {//
                    var hidInput = data +"<input type='hidden' value = '"+ row.exceptReason +"' />"
                    return hidInput;
                }
            },{
                "data": "isOrderSku",
                "className": "text-left",
                "orderable": false
            }, {
                "data": "isGift",
                "className": "text-left",
                "orderable": false
            }, {
                "data": "goodsCode",
                "className": "truncate-text tooltip-column-detail text-left",
                "orderable": false
            }, {
                "data": "goodsName",
                "className" : "truncate-text tooltip-column-detail text-left",
                "orderable": false
            }, {
                "data": "skuCode",
                "className" : "truncate-text tooltip-column-detail text-left",
                "orderable": false
            }, {
                "data": "skuName",
                "className" : "truncate-text tooltip-column-detail text-left",
                "orderable": false
            }, {
                "data": "qty",
                "className" : "text-right",
                "orderable": false
            }, {
                "data": "factUnitPrice",
                "className" : "text-right",
                "orderable": false,
                "render": numberParser,
            }, {
                "data": "factMoney",
                "className" : "text-right",
                "orderable": false,
                "render": numberParser,
            },{
                "data": "goodsSkuCost",
                "className" : "text-right",
                "orderable": false,
                "render": numberParser,
            }, {
                "data": "remark",
                "className": "text-left",
                "orderable": false
            }, {
                "data": "sn",
                "className" : "truncate-text tooltip-column-detail",
                "orderable": false
            }
            ]
        }).on('draw.dt',function(){
            $("#sysReturnSkuTable").tooltip({
                selector : ".tooltip-column-detail",
                container : "body",
                title : function() {
                    if($(this)[0].children[1] == undefined){
                        if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                            return $(this).text();
                        }
                    }else{
                        return $(this)[0].children[1].value;
                    }
                }
            });
        });
    }
    $sysReturnSkuTable.rows.add(pageData.sysReturnSku).draw();
};


sysReturnDetail.initFactReturnSku = function (pageData) {
    // 根据退货ID初始化实际退入商品明细列表
    if (null == $sysFactReturnSkuTable) {
        $sysFactReturnSkuTable = $("#sysFactReturnSkuTable").DataTable({
            "processing": true,
            "dom": '<"bottom"rtflp>',
            "searching": false,
            "scrollCollapse": false,
            "destroy": true,
            "paging": false,
            "deferRender": true,
            "ordering": false,
            "columns": [{
                "data": "isGift",
                "className": "text-left"
            }, {
                "data": "goodsCode",
                "className" : "truncate-text tooltip-column-detail text-left"
            }, {
                "data": "goodsName",
                "className" : "truncate-text tooltip-column-detail text-left"
            }, {
                "data": "skuCode",
                "className" : "truncate-text tooltip-column-detail text-left"
            }, {
                "data": "skuName",
                "className" : "truncate-text tooltip-column-detail text-left"
            }, {
                "data": "qty",
                "className": "text-right",
                "orderable": false
            }, {
                "data": "inventoryType",
                "className": "text-left",
                "orderable": false
            }, {
                "data": "factUnitPrice",
                "className": "text-right",
                "orderable": false,
                "render": numberParser,
            }, {
                "data": "factMoney",
                "className": "text-right",
                "render": numberParser
            }, {
                "data": "divideGoodsFee",
                "className": "text-right",
                "orderable": false,
                "render": numberParser,
            }, {
                "data": "divideOrderFee",
                "className": "text-right",
                "orderable": false,
                "render": numberParser,
            }, {
                "data": "goodsSkuCost",
                "className": "text-right",
                "orderable": false,
                "render": numberParser,
            },{
                "data": "sn",
                "orderable": false
            }]
        }).on('draw.dt',function(){
            $("#sysFactReturnSkuTable").tooltip({
                selector : ".tooltip-column-detail",
                container : "body",
                title : function() {
                	if($(this)[0].children[1] == undefined){
                		if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                            return $(this).text();
                        }
                	}else{
                		return $(this)[0].children[1].value;
                	}
                }
            });
        });
    }
    $sysFactReturnSkuTable.rows.add(pageData.sysFactReturnSkuList).draw();
};

sysReturnDetail.getDetailInfoByReturnId = function () {
    var returnId = extractParam("returnId");
    if (!!returnId) {
        return sysReturnDetail.operation.getRefundInfo(returnId);
    }
};

/*
 * 获取退货单的明细信息，包括基本信息，计划退入商品，实际退入商品
 */
sysReturnDetail.operation.getRefundInfo = function (sysReturnId) {
    var returnData = null;
    $.ajax({
        type: "GET",
        url: "/sysReturnManager/sysReturnFullDetail.action",
        contentType: "application/json;charset=utf-8",
        data: {
            "sysReturnId": sysReturnId
        },
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.result == "success") {
                returnData = data.data;
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                window.top.window.showScoMessage('error', result.errorObject.errorText);
            }
        }
    });

    var item = {};
    for (var i = 0, j = returnData.sysReturnSku.length; i < j; i++) {
        item = returnData.sysReturnSku[i];
        item.factMoney = item.qty * item.factUnitPrice;
        item.divideOrderFee = item.qty * item.divideGoodsFee;
        if (item.isGift == "y") {
            item.isGift = "是";
        } else {
            item.isGift = "否";
        }
        if (item.isOrderSku == "y") {
            item.isOrderSku = "订单";
        } else if (item.isOrderSku == "r") {
            item.isOrderSku = "补发单";
        }
        else {
            item.isOrderSku = "非单据";
        }
    }
    var item1 = {};
    for (var i = 0, j = returnData.sysFactReturnSkuList.length; i < j; i++) {
        item1 = returnData.sysFactReturnSkuList[i];
        item1.factMoney = item1.qty * item1.factUnitPrice;
        item1.divideOrderFee = item1.qty * item1.divideGoodsFee;
        if (item1.inventoryType == "zp") {
            item1.inventoryType = "正品";
        } else {
            item1.inventoryType = "次品";
        }
        if (item1.isGift == "y") {
            item1.isGift = "是";
        } else {
            item1.isGift = "否";
        }
    }
    return returnData;
}


// 初始化内部便签
sysReturnDetail.initNotePage = function(){
    var returnId = extractParam("returnId");
    var ifmSrc = "/views/base/note/notelist.html?busiKey=" + returnId;
    $("#noteIframe").attr("src",ifmSrc);
};



// ====== 公共方法 ======
//获取页面参数
function GetArgsFromHref(sHref, sArgName) {
    var args = sHref.split("?");
    var retval = "";

    if (args[0] == sHref) /*参数为空*/
    {
        return retval;
        /*无需做任何处理*/
    }
    var str = args[1];
    args = str.split("&");
    for (var i = 0; i < args.length; i++) {
        str = args[i];
        var arg = str.split("=");
        if (arg.length <= 1) continue;
        if (arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}

function extractParam(paramName) {
    var returnString = "";
    var str = window.location.search;
    if (!!str) {
        returnString = GetArgsFromHref(str, paramName);
    }
    return returnString;
}
var numberParser = function(data){
    var returnValue = "-";
    var numberData = parseFloat(data);
    if(numberData===+numberData){
        returnValue = numberData.toFixed(2);
    }
    return returnValue;
};